package com.supermarket.order.service.impl;

import com.supermarket.common.security.utils.SecurityUtils;
import com.supermarket.order.domain.OrderMaster;
import com.supermarket.order.domain.OrderOperationLog;
import com.supermarket.order.dto.OrderDeliveryDTO;
import com.supermarket.order.dto.OrderQueryDTO;
import com.supermarket.order.dto.UpdateRemarkDTO;
import com.supermarket.order.mapper.OrderItemMapper;
import com.supermarket.order.mapper.OrderMasterMapper;
import com.supermarket.order.mapper.OrderOperationLogMapper;
import com.supermarket.order.service.OrderService;
import com.supermarket.order.vo.OrderDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMasterMapper orderMasterMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderOperationLogMapper orderOperationLogMapper;

    @Override
    public List<OrderMaster> getOrderList(OrderQueryDTO queryDTO) {
        List<OrderMaster> orderMasters = orderMasterMapper.selectOrderList(queryDTO);
        return orderMasters;
    }

    @Override
    public OrderDetailVO getOrderDetail(Long orderId) {
        OrderDetailVO vo = new OrderDetailVO();
        // 1. 查询订单主信息
        OrderMaster orderMaster = orderMasterMapper.selectOrderById(orderId);
        if (orderMaster != null) {
            BeanUtils.copyProperties(orderMaster, vo);
            // 2. 查询订单商品
            vo.setOrderItems(orderItemMapper.selectByOrderId(orderId));
            // 3. 查询操作日志
            vo.setOperationLogs(orderOperationLogMapper.selectByOrderId(orderId));
        }
        return vo;
    }

    @Override
    @Transactional
    public void closeOrder(Long orderId, String note) {
        OrderMaster order = orderMasterMapper.selectOrderById(orderId);
        if (order != null && order.getOrderStatus() == 0) { // 假设0是待付款状态
            order.setOrderStatus(4); // 假设4是已关闭状态
            orderMasterMapper.updateOrderMaster(order);
            logOperation(orderId, "关闭订单", note);
        }
    }

    @Override
    @Transactional
    public void deliverOrder(OrderDeliveryDTO deliveryDTO) {
        OrderMaster order = orderMasterMapper.selectOrderById(deliveryDTO.getOrderId());
        if (order != null && order.getOrderStatus() == 1) { // 假设1是待发货
            order.setOrderStatus(2); // 假设2是已发货
            // 省略了物流信息更新，若依框架通常有物流模块，这里简化
            order.setDeliveryTime(new Date());
            orderMasterMapper.updateOrderMaster(order);
            logOperation(deliveryDTO.getOrderId(), "订单发货", "物流公司：" + deliveryDTO.getDeliveryCompany() + "，单号：" + deliveryDTO.getDeliverySn());
        }
    }
    
    @Override
    @Transactional
    public void updateRemark(UpdateRemarkDTO remarkDTO) {
        OrderMaster order = orderMasterMapper.selectOrderById(remarkDTO.getOrderId());
        if (order != null) {
            order.setNote(remarkDTO.getRemark());
            orderMasterMapper.updateOrderMaster(order);
            logOperation(remarkDTO.getOrderId(), "修改备注", remarkDTO.getRemark());
        }
    }

    private void logOperation(Long orderId, String operationType, String remark) {
        OrderOperationLog log = new OrderOperationLog();
        log.setOrderId(orderId);
        log.setOperationType(getOperationType(operationType));
        log.setOperator(SecurityUtils.getUsername());
        log.setBeforeData(""); // 简化处理，可使用JSON序列化记录前后数据
        log.setAfterData(remark);
        orderOperationLogMapper.insertLog(log);
    }
    
    private int getOperationType(String type) {
        switch(type) {
            case "关闭订单": return 2;
            case "订单发货": return 3;
            case "修改备注": return 4; // 根据你的表设计自定义
            default: return 0;
        }
    }
} 